//vue
const app = new BaseValidateVue({
    el: '#app',
    data: function () {
        return {
            formData: {
                houseId: '',
                houseArea: '',
                frameCode: '',
                frameName: '',
                cabinetLocation: '',
                useType: '1',
                distribution: '1',
                occupancy: '1',
                positions: [
                    new BasePosition(),
                ],
            },
            areas: [],
            houses: [],
            validateList: [
                new ValidateObject({
                    ruleName: 'frame_code_validate',
                    validate: this.frameCodeValidate,
                }),
                new ValidateObject({
                    ruleName: 'position_number_validate',
                    validate: this.positionNumberValidate,
                }),
            ],
        }
    },
    methods: {
        createValidateList() {
            return this.validateList
        },
        async submitForm() {
            if (await this.$validator.validateAll() && await ajaxOption({
                res: baseFrameInfoApi.add(this.formData),
                danger: '新增失败',
                success: '新增成功',
            })) {
                forward('/basic-data/admin_frame_info')
            } else {
                await rxPrompt.dangerMsg('输入存在错误！')
            }
        },
        async initHouses() {
            this.houses = await baseHouseInfoApi.selectList()
        },
        addPositionInfo() {
            this.formData.positions.push(new BasePosition())
        },
        deletePositionInfo(index) {
            this.formData.positions.splice(index, 1)
        },
        async changeHouse() {
            this.areas = await baseFrameInfoApi.listByHouseId(this.formData.houseId)
        },
        async frameCodeValidate(val, args) {
            const {houseArea = ''} = this.formData
            if (houseArea === '' || val === '') {
                return true
            }
            return await baseFrameInfoApi.selectFrameNumberByAreaId(houseArea, val)
        },
        async positionNumberValidate() {
            const positionNumbers = this.formData.positions.map(obj => obj.positionNumber)
            let flagArray = []
            for (var i = 0; i < positionNumbers.length; i++) {
                if (flagArray[positionNumbers[i]]) {
                    return false
                }
                flagArray[positionNumbers[i]] = true
            }
            return true
        },
    },
    mounted: function () {
        this.initHouses()
        this.deletePositionInfo()
    },
})
